﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace  Game.TDBehaviorTree
{
   public class JsonDataParser
    {

        public static void WriteJson(object t, string file)
        {
            string _path = UnityEngine.Application.dataPath + "/StreamingAssets/BehaviorData";
            string filePath = _path + "/" + file;
            if (!Directory.Exists(_path))
                Directory.CreateDirectory(_path);
            if (File.Exists(filePath))
                File.Delete(filePath);
            UnityEditor.AssetDatabase.Refresh();


            JsonSerializer serializer = new JsonSerializer();
            using (StreamWriter sw = new StreamWriter(filePath))
            {
                using (Newtonsoft.Json.JsonWriter writer = new JsonTextWriter(sw))
                {
                    serializer.Serialize(writer, t);
                }
            }
        }

        public static void ReadJson<T>(string file,out T obj)
        {

            string fullPath = UnityEngine.Application.streamingAssetsPath + @"/BehaviorData/" + file + ".json";
            JsonSerializer serializer = new JsonSerializer();
            using (StreamReader sr = new StreamReader(fullPath))
            {
                obj = JsonConvert.DeserializeObject<T>(sr.ReadToEnd(), new JsonDemoConverter());
            }
        }

        public abstract class JsonCreationConverter<T> : JsonConverter
        {
            protected abstract T Create(Type objectType, JObject jsonObject);
            public override bool CanConvert(Type objectType)
            {
                return typeof(T).IsAssignableFrom(objectType);
            }

           
            public override object ReadJson(Newtonsoft.Json.JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
            {
                var jsonObject = JObject.Load(reader);
                var target = Create(objectType, jsonObject);
                serializer.Populate(jsonObject.CreateReader(), target);
                return target;
            }

            public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, JsonSerializer serializer)
            {
                throw new NotImplementedException();
            }
        }



        /// <summary>
        /// 根据节点类型返回
        /// </summary>
        public class JsonDemoConverter : JsonCreationConverter<NodeBase>
        {
            protected override NodeBase Create(Type objectType, JObject jsonObject)
            {
                NodeType nodeType =(NodeType)int.Parse(jsonObject["nodeType"].ToString());
                int leafType = int.Parse(jsonObject["nodeLeafType"].ToString());
                NodeBase _node= TDBehaviorBT.GetNode(nodeType, leafType);
                return _node;
            }
        }

    }
}
